#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    Base(long x)
    : _base(x)
    {
        /* cout << "Base()" << endl; */
    }
    
    Base(const Base & rhs)
    : _base(rhs._base)
    {
        cout << "Base的拷贝构造函数" << endl;
    }

    Base& operator=(const Base & rhs){
        cout << "Base的赋值运算符函数" << endl;
        _base = rhs._base;
        return *this;
    }
    
    void print() const{
        cout << "_base:" << _base << endl;
    }

private:
    long _base;
};

class Derived
: public Base
{
public:
    Derived(long base,long derived)
    : Base(base)
    , _derived(derived)
    {
        /* cout << "Derived()" << endl; */
    }

    Derived(const Derived & rhs)
    : Base(rhs)//显式调用Base类的拷贝构造函数
    , _derived(rhs._derived)
    {
        cout << "Derived的拷贝构造函数" << endl;
    }
    
    Derived & operator=(const Derived & rhs){
        Base::operator=(rhs);//显式调用Base类的赋值运算符函数
        _derived = rhs._derived;
        cout << "Derived的赋值运算符函数" << endl;
        return *this;
    }

    void print() const{
        Base::print();
        cout << "_derived:" << _derived << endl;
    }
private:
    long _derived;
};

#if 0
void test0(){
    Base base(10);
    Derived d1(4,6);

    //底层是base对象调用了Base类的赋值运算符函数
    //base.operator=(d1);
    //形参 const Base & rhs = d1
    base = d1;//ok

    cout << base._base << endl;
}
#endif

void test1(){
    Derived d1(6,9);

    Derived d2(5,7);
    d1 = d2;
    d1.print();

    cout << endl;
    Derived d3 = d2;
    d3.print();
}


int main(void){
    test1();
    return 0;
}
